﻿using System;

namespace Storm.TextEditor.Editor.Text
{
    /// <summary>
    /// Represents a range of text in a document.
    /// </summary>
    public class TextRange
    {
        #region Fields

        private int firstRow = 0;
        private int firstColumn = 0;

        private int lastRow = 0;
        private int lastColumn = 0;

        #region Events

        /// <summary>
        /// Occurs when the bounds of the TextRange has changed.
        /// </summary>
        public event EventHandler Change = null;

        #endregion

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the start column of the TextRange.
        /// </summary>
        public int FirstColumn
        {
            get { return firstColumn; }
            set
            {
                firstColumn = value;
                this.OnChange();
            }
        }

        /// <summary>
        /// Gets or sets the start row of the TextRange.
        /// </summary>
        public int FirstRow
        {
            get { return firstRow; }
            set
            {
                firstRow = value;
                this.OnChange();
            }
        }

        /// <summary>
        /// Gets or sets the end column of the TextRange.
        /// </summary>
        public int LastColumn
        {
            get { return lastColumn; }
            set
            {
                lastColumn = value;
                this.OnChange();
            }
        }

        /// <summary>
        /// Gets or sets the end row of the TextRange.
        /// </summary>
        public int LastRow
        {
            get { return lastRow; }
            set
            {
                lastRow = value;
                this.OnChange();
            }
        }

        #endregion

        #region Methods

        #region Public

        /// <summary>
        /// Sets the bounds of the TextRange to the given values.
        /// </summary>
        /// <param name="firstColumn">Start column of the new bounds.</param>
        /// <param name="firstRow">Start row of the new bounds.</param>
        /// <param name="lastColumn">End column of the new bounds.</param>
        /// <param name="lastRow">End row of the new bounds.</param>
        public void SetBounds(int firstColumn, int firstRow, int lastColumn, int lastRow)
        {
            this.firstColumn = firstColumn;
            this.firstRow = firstRow;

            this.lastColumn = lastColumn;
            this.lastRow = lastRow;

            this.OnChange();
        }

        #endregion

        #region Protected

        /// <summary>
        /// Raises the OnChange event.
        /// </summary>
        protected virtual void OnChange()
        {
            if (this.Change != null)
                this.Change(this, EventArgs.Empty);
        }

        #endregion

        #endregion

        /// <summary>
        /// Initializes a new instance of TextRange.
        /// </summary>
        public TextRange()
        {
        }

        /// <summary>
        /// Initializes a new instance of TextRange.
        /// </summary>
        /// <param name="firstColumn">Start column of the TextRange.</param>
        /// <param name="firstRow">Start row of the TextRange.</param>
        /// <param name="lastColumn">End column of the TextRange.</param>
        /// <param name="lastRow">End row of the TextRange.</param>
        public TextRange(int firstColumn, int firstRow, int lastColumn, int lastRow)
        {
            this.firstColumn = firstColumn;
            this.firstRow = firstRow;
            this.lastColumn = lastColumn;
            this.lastRow = lastRow;
        }
    }
}
